package com.jial08.tools.common.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author zyy
 * @Description
 * @Date 2019/1/4
 * @Param
 * @return
 **/
@Component
@Slf4j
public class RedisUtil {

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 批量删除对应的 key-value
     * @param keys
     */
    public void remove(final Object... keys) {
        for (Object key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     * @param pattern
     */
    public void removePattern(final Object pattern) {
        Set<Object> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 删除对应的value
     * @param key
     */
    public void remove(final Object key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     * @param key
     * @return
     */
    public boolean exists(final Object key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     * @param key
     * @param <T>
     * @return
     */
    public <T> T get(final Object key) {
        Object result = redisTemplate.opsForValue().get(key);
        if (result == null) {
            return null;
        }
        return (T) result;
    }

    /**
     * 写入缓存
     * @param key
     * @param value
     */
    public void set(final Object key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 写入缓存设置过期时间
     * @param key
     * @param value
     * @param expireTime
     * @param timeUnit
     */
    public void set(final Object key, Object value, long expireTime, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, expireTime, timeUnit);
    }

    /**
     * 获取list缓存的长度
     * @param key
     * @return
     */
    public long getListSize(final Object key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 通过索引 获取list中的值
     * @param key   键
     * @param index 索引  index>=0时， 0 表头，1 第二个元素，依次类推；index<0时，-1，表尾，-2倒数第二个元素，依次类推
     * @return
     */
    public Object listGetIndex(final Object key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据索引修改list中的某条数据
     *
     * @param key   键
     * @param index 索引
     * @param value 值
     * @return
     */
    public boolean listUpdateIndex(Object key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 一定时间后移除 key 对应的 value
     * @param key
     * @param time
     * @param timeUnit
     * @return
     */
    private Boolean expire(Object key, long time, TimeUnit timeUnit) {
        return redisTemplate.expire(key, time, timeUnit);
    }

}